<!--景区-->
<template>
  <div class="scenic-spot-list">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="景区名字">
        <el-input v-model="formInline.scenicSpotName" placeholder="名字"></el-input>
      </el-form-item>
      <el-form-item label="行政区">
        <el-input v-model="formInline.district" placeholder="行政区"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="line"></div>
  <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column
      label="创建日期"
      width="180">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{ scope.row.createdTime }}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="景区名称"
      width="180">
      <template slot-scope="scope">
        <el-popover trigger="hover" placement="top">
          <p>详细介绍: {{ scope.row.introduction }}</p>
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.scenicSpotName }}</el-tag>
          </div>
        </el-popover>
      </template>
    </el-table-column>

    <el-table-column
      label="行政区"
      width="180">
      <template slot-scope="scope">
        <div slot="reference" class="name-wrapper">
          <el-tag size="medium">{{ scope.row.district }}</el-tag>
        </div>
      </template>
    </el-table-column>

    <el-table-column
      label="简介"
      width="180">
      <template slot-scope="scope">
        <div slot="reference" class="name-wrapper">
          <el-tag size="medium">{{ scope.row.introduction }}</el-tag>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑
        </el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
    <el-pagination
      v-if="pageInfo.total>0"
      :current-page="pageInfo.page_index"
      @current-change="getProfile"
      :total="pageInfo.total"
      :page-size="pageInfo.page_size"
      :page-sizes="pageInfo.page_sizes"
      class="list-page"
      background
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>
<script>
export default {
  name: 'scenicSpotList',
  components: {},
  data () {
    return {
      formInline: {
        scenicSpotName: '',
        district: ''
      },
      pageInfo: {
        page_index: 1, // 当前位于哪页
        total: 0, // 总数
        page_size: 15, // 1页显示多少条
        page_sizes: [5, 10, 15, 20], // 每页显示多少条
        layout: 'total, sizes, prev, pager, next, jumper' // 翻页属性
      },
      tableData: [{
        id: '',
        scenicSpotName: '',
        introduction: '',
        address: '',
        district: '',
        createdTime: ''
      }]
    }
  },
  created () {
    this.getProfile()
  },
  methods: {
    getProfile (curPage) {
      if (!curPage) {
        curPage = 1
      }
      // 获取表格数据
      this.$axios.get('/scenic-spots', {
        params: {
          name: this.formInline.scenicSpotName,
          district: this.formInline.district,
          pageNum: curPage,
          pageSize: 10
        }
      }).then(res => {
        // this.tableData = res.data;
        console.log(res.data)
        this.tableData = res.data.data.list
        // 设置分页数据
        this.setPaginations(res.data.data)
      })
    },
    query () {
      this.getProfile()
    },
    handleEdit (index, row) {
      this.$router.push({ path: '/scenic-spot-update', query: { id: row.id } })
    },
    handleDelete (index, row) {
      this.$axios.delete(`/scenic-spots/${row.id}`).then( res=>{
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.$router.go(0);
      })

    },
    setPaginations (page) {
      // 总页数
      this.pageInfo.page_index = page.pageNum
      this.pageInfo.page_size = page.pageSize
      this.pageInfo.total = page.total
      this.pageInfo.page_sizes = page.size
    }
  }
}
</script>
<style>
  .line{
    height: 10px;
    border-top: 1px solid #ddd;
    text-align: center;
  }
</style>
